import {
	fileURLToPath,
	URL
} from 'node:url'
import {
	defineConfig, loadEnv 
} from 'vite'
import uni from '@dcloudio/vite-plugin-uni'
import AutoImport from 'unplugin-auto-import/vite'
import eslintPlugin from 'vite-plugin-eslint'
// https://vitejs.dev/config/
/*

,
    "h5" : {
        "title" : "E-selina",
        "router" : {
            "mode" : "history",
            "base" : "/h5/"
        }
    }

"h5": {
		"router": {
			"mode": "history"
		},
		"title": "Selina APP",
		"template": "index.html",
		"devServer": {
			"port": 8080,
			// "disableHostCheck": true,
			"proxy": {
				"/baseapi": {
					"target": "http://106.55.78.26:1280", //测试环境
					"changeOrigin": true,
					"ws": true,
					"secure": true,
					"pathRewrite": {
						"^/baseapi": ""
					}
				}
			}
		}
	}
*/

export default ({ command, mode })=>{
	const env = loadEnv(mode, process.cwd());
	console.log("env.VITE_BASE",env.VITE_BASE);
	return defineConfig({
		base: env.VITE_BASE,
		 // 可以配置设置了基础路径和没有配置基础路径打包文件夹不一样，容易区分
		// outDir: env.VITE_BASE && env.VITE_BASE.length > 2 ? 'baseDist' : 'dist',
		server: {
			host: "192.168.1.5", // 指定服务器应该监听哪个IP地址,默认：localhost
			port: 5173, // 指定开发服务器端口,默认：5173
			proxy: { // 为开发服务器配置自定义代理规则
				// 带选项写法：http://localhost:5173/api/posts -> http://jsonplaceholder.typicode.com/posts
				"/baseapi": {
					target: "http://106.55.78.26:1280", // 目标接口
					changeOrigin: true, // 是否换源
					rewrite: (path) => path.replace(/^\/baseapi/, ""),
				}
			}
		},
		plugins: [
			uni(),
			// eslintPlugin({
			//   cache: true,
			//   include: ['src/**/*.js', 'src/**/*.vue', 'src/*.js', 'src/*.vue', 'src/*.nvue'],
			//   failOnError: false // eslint报错不影响运行
			// }),
			AutoImport({
				// targets to transform
				include: [
					/\.[j]sx?$/,
					/\.vue$/,
					/\.vue\?vue/,
					/\.nvue$/,
					/\.nvue\?nvue/,
					/\.md$/
				],
	
				// global imports to register
				imports: [
					// 插件预设支持导入的api
					'vue',
					'pinia',
					'uni-app'
				],
				eslintrc: {
					enabled: true, // Default `false`
					filepath: './.eslintrc-auto-import.json', // Default `./.eslintrc-auto-import.json`
					globalsPropValue: true // Default `true`, (true | false | 'readonly' | 'readable' | 'writable' | 'writeable')
				},
				dts: './auto-imports.d.ts'
				// dts:false
			})
		],
		resolve: {
			alias: {
				'@': fileURLToPath(new URL('./src', import.meta.url))
			}
		},
		transpileDependencies: ['@dcloudio/uni-ui']
	})
}